package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-20
 */
//课程分类管理中的--课程分类列表
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //使用excel添加课程的service方法
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {

        try {
            //文件输入流
            InputStream in=file.getInputStream();

            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //课程分类树形结构service方法
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.查询所有的一级分类 parentId=0
        QueryWrapper<EduSubject> oneQuery=new QueryWrapper<>();
        oneQuery.eq("parent_id","0");
        List<EduSubject> oneList=baseMapper.selectList(oneQuery);
        //2.查询所有的二级分类 parentId!=0
        QueryWrapper<EduSubject> twoQuery=new QueryWrapper<>();
        twoQuery.ne("parent_id","0");
        List<EduSubject> twoList=baseMapper.selectList(twoQuery);

        //创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubjectList=new ArrayList<>();

        //3.封装一级分类
        //查询出来所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值
        //封装到要求的list集合里面 List<OneSubject> finalSubjectList
        for(EduSubject eduSubject:oneList){
            OneSubject oneSubject=new OneSubject();
            oneSubject.setId(eduSubject.getId());
            oneSubject.setTitle(eduSubject.getTitle());
            finalSubjectList.add(oneSubject);
            //下面的代码等价于上面的代码过程
            //OneSubject oneSubject=new OneSubject();
            //BeanUtils.copyProperties(eduSubject,oneSubject)
            //finalSubjectList.add(oneSubject);

            //4.封装二级分类
            List<TwoSubject> twoFinalSubjectList=new ArrayList<>();
            //遍历二级分类list集合
            for(EduSubject tSubject:twoList){
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject=new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            //把二级分类中的所有对象装到一级分类中
            oneSubject.setChildren(twoFinalSubjectList);
        }
        return finalSubjectList;
    }
}
